package com.gengzp.rpc.config;

import com.gengzp.rpc.tools.RpcEnvironmentVariableReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.net.http.HttpClient;
import java.time.Duration;

import static com.gengzp.rpc.constants.RpcBeanNameConstants.RPC_ENVIRONMENT_VARIABLE_READER;

/**
 * @ClassName RpcHttpClientConfig
 * @Description rpc框架内置 httpClient 配置类
 * @Author gengzp
 * @Date 2025/9/24 17:45
 */
@Configuration
@DependsOn(RPC_ENVIRONMENT_VARIABLE_READER)
public class RpcHttpClientConfig {

    /**
     * rpc框架内置 Java HttpClient
     */
    @Bean
    public HttpClient rpcHttpClient() {
        return HttpClient.newBuilder()
                // 连接超时设置
                .connectTimeout(Duration.ofSeconds(RpcEnvironmentVariableReader.getRpcTimeout()))
                // 启用HTTP/2支持
                .version(HttpClient.Version.HTTP_2)
                // 跟随重定向
                .followRedirects(HttpClient.Redirect.NORMAL)
                .build();
    }

}
